/**
 * Created by Jie on 2017/2/6.
 */
var async = require('async');
var db = require('../../util/pool');
var format = require('../../util/json2sql');
var data = require('../../util/result');
var _ = require('underscore');

var Model = function() {};
var _table = 'role';

//检查Name是否存在
Model.prototype.checkName = function (_where,callback) {
    var sql = format.check(_table,_where);
    db.queryFirst(sql,function (err,result) {
        if(!err){
            if((typeof result) == 'object'){
                callback(null,data.result(1,'角色名称已经存在'));
            }else {
                callback(null,data.result(0,'ok'));
            }
        }else {
            callback(null,data.result(1,'异常'));
        }
    })
}

//通过id获取数据
Model.prototype.one = function (_id,callback) {
    var sql = format.one(_table,_id);
    db.queryFirst(sql,function (err,results) {
        if(!err){
            callback(null,data.result(0,'ok',results));
        }else{
            callback(null,data.result(1,'异常'));
        }
    })
}
//添加数据
Model.prototype.insert = function(json, callback) {
    var sql = format.insert(_table,json);
    db.exec(sql,function (err,results) {
        if(!err){
            callback(null,data.result(0,'数据新增操作成功',results));
        }else{
            callback(null,data.result(1,'异常'));
        }
    });
};
//编辑数据
Model.prototype.edit = function (json,callback) {
    var sql = format.update(_table,json);
    db.exec(sql,function (err,results) {
        if(!err){
            callback(null,data.result(0,'数据修改操作成功',results));
        }else{
            callback(null,data.result(1,'异常'));
        }
    })
}
//删除数据
Model.prototype.del = function (params,callback) {
    var sql = format.delete(_table,params);
    db.exec(sql,function (err,results) {
        if(!err){
            callback(null,data.result(0,'数据删除操作成功',results));
        }else{
            callback(null,data.result(1,'异常'));
        }
    })
}
//数据列表
Model.prototype.list = function(where, callback) {
    var countSql = format.count(_table,where);
    var dataSql = '';
    var idsSql = format.ids(_table,where);

    async.waterfall([
        function(callback) {
            db.queryFirst(countSql,function (err,results) {
                if(!err){
                    callback(null,results);
                }else{
                    callback(null,null);
                }
            });
        },
        function(vals, callback) {
            db.query(idsSql,function (err,results) {
                var d = {total:vals.rows};
                if(!err){
                    d.ids = results;
                    callback(null,d);
                }else{
                    d.ids = [];
                    callback(null,d);
                }
            });
        },
        function(vals, callback) {
            try{
                var ids = [];
                var idsArg = vals.ids;
                for(var i=0;i<idsArg.length;i++){
                    ids.push(idsArg[i].id);
                }
                dataSql = format.select(_table,where,ids.join(','));
                db.query(dataSql,function (err,results) {
                    if(!err){
                        var d = {error:0,data:results,total:vals.total};
                        callback(null,d);
                    }else {
                        callback(null,{error:1});
                    }
                });
            }catch (e){
                callback(null,{error:1});
            }
        }
    ], function(error, result) {
        callback(null, result);
    });
};

//读取所有数据
Model.prototype.all = function(_cond, callback) {
    var sql = format.all(_table,_cond.where,_cond.order);
    db.query(sql,function (err,results) {
        if(!err){
            callback(null,data.result(0,'ok',results));
        }else{
            callback(null,data.result(1,'异常'));
        }
    })
};

module.exports = Model;